package matrixCalculator;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

import Jama.Matrix;
/**
 * IO Thread for per object Architecture, it works petitions with threads that does not  share files
 * **/
public class PerPetitionIOObject extends Thread  {
	
	
	private Vector<MatrixOperation> matrixOperationBuffer;
	private Vector<MatrixOperation> matrixResultsBuffer;

	public PerPetitionIOObject(Vector<MatrixOperation> matrixOperationBuffer, Vector<MatrixOperation> matrixResultsBuffer){
		this.matrixOperationBuffer=matrixOperationBuffer;
		this.matrixResultsBuffer=matrixResultsBuffer;
		
	}
	
	public void run(){
		while(true){
			if(!matrixOperationBuffer.isEmpty()){
				MatrixOperation operation = matrixOperationBuffer.remove(0);
				CalculatorThreadObject cThread = new CalculatorThreadObject(operation);
				cThread.run();
				try {
					cThread.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				matrixResultsBuffer.add(operation);
				
			}
			
			if(!matrixResultsBuffer.isEmpty()){
				MatrixOperation operation = matrixResultsBuffer.remove(0);
				
				DataOutputStream out= operation.getOut();
				try {
					String resultS = operation.getR();
					
					resultS = (operation.getN()+"_" + operation.getOperator() + "\nrwTime_"+ operation.getHddTime()+ "\npTime_"+operation.getProcessTime() +"\n" + resultS+"\n" );
					
					out.writeBytes(resultS);
					out.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//write result
				
			}
			
		}
		
	}
	
	
}
